package com.qingke.pms.factory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.pms.command.Command;
import com.qingke.pms.command.user.AddCompanyCommand;
import com.qingke.pms.command.user.AddMaterialCommand;
import com.qingke.pms.command.user.AddProjectCommand;
import com.qingke.pms.command.user.AddProjectLeaderCommand;
import com.qingke.pms.command.user.AlterMaterialCommand;
import com.qingke.pms.command.user.AlterProjectCommand;
import com.qingke.pms.command.user.AlterUserCommand;
import com.qingke.pms.command.user.ApproveCommand;
import com.qingke.pms.command.user.IntoProjectCommand;
import com.qingke.pms.command.user.ListMaterialsCommand;
import com.qingke.pms.command.user.ListProjectsCommand;
import com.qingke.pms.command.user.ListUsersCommand;
import com.qingke.pms.command.user.UserHelpCommand;
import com.qingke.pms.pojo.User;

public class UserCommandFactory extends SystemCommandFactory{
	
	private static Map<CommandCode,Class<? extends Command>> userMap=new HashMap<>();
	
	
	static{
		userMap.put(CommandCode.ADDCOMPANY, AddCompanyCommand.class);
		userMap.put(CommandCode.ADDMATERIAL, AddMaterialCommand.class);
		userMap.put(CommandCode.ADDPROJECT, AddProjectCommand.class);
		userMap.put(CommandCode.ADDPROJECTLEADER, AddProjectLeaderCommand.class);
		userMap.put(CommandCode.ALTERMATERIAL, AlterMaterialCommand.class);
		userMap.put(CommandCode.ALTERPROJECT, AlterProjectCommand.class);
		userMap.put(CommandCode.ALTERUSER, AlterUserCommand.class);
		userMap.put(CommandCode.APPROVE, ApproveCommand.class);
		userMap.put(CommandCode.LISTMATERIALS, ListMaterialsCommand.class);
		userMap.put(CommandCode.LISTPROJECTS, ListProjectsCommand.class);
		userMap.put(CommandCode.LISTUSERS, ListUsersCommand.class);
		userMap.put(CommandCode.HELP, UserHelpCommand.class);
		userMap.put(CommandCode.INTO, IntoProjectCommand.class);
	}
	
	private User user;
	public UserCommandFactory(User user){
		this.user=user;
	}
	
	public User getUser() {
		return user;
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<? extends Command> commandClass=null;
		commandClass=userMap.get(cmdCode);
		if(commandClass==null)
			return super.buildCommand(cmdCode);
		
		Command command=null;
		try {
			command=commandClass.getConstructor(User.class).newInstance(this.getUser());
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return command;
	}
}
